<?php
namespace app\command;

use app\common\model\ShopAllianceModel;
use app\common\model\SystemCityModel;
use TencentCloud\Cdb\V20170320\Models\VerifyRootAccountRequest;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class Hello extends Command
{
    protected function configure()
    {
        $this->setName('hello')
            ->addArgument('name', Argument::OPTIONAL, "your name")
            ->addOption('city', null, Option::VALUE_REQUIRED, 'city name')
            ->setDescription('Say Hello');
    }

    protected function execute(Input $input, Output $output)
    {
        $name = trim($input->getArgument('name'));
        $name = $name ?: 'thinkphp';
        $where=[];
        $res_data=SystemCityModel::list($where,['a.id'=>'asc'],1,100000);
        foreach ($res_data as $v){
            $initials=$this->getFirstChar($v['name']);
            if(!empty($initials)){
                SystemCityModel::update(['initials'=>$initials],['id'=>$v['id']]);
            }
        }
        $output->writeln("end"  );
    }
    function getFirstChar($str){
        $str = iconv("UTF-8","gb2312//IGNORE", $str); // 转换为 GB2312 编码
        $fchar=0;
        if(!empty($str[0]) && !empty($str[1])){
            $fchar = ord($str[0]) * 256 + ord($str[1]) - 65536; // 多字节字符获取 Unicode 码值
        }
        if ($fchar >= -20319 and $fchar <= -20284) return "A";
        if ($fchar >= -20283 and $fchar <= -19776) return "B";
        if ($fchar >= -19775 and $fchar <= -19219) return "C";
        if ($fchar >= -19218 and $fchar <= -18711) return "D";
        if ($fchar >= -18710 and $fchar <= -18527) return "E";
        if ($fchar >= -18526 and $fchar <= -18240) return "F";
        if ($fchar >= -18239 and $fchar <= -17923) return "G";
        if ($fchar >= -17922 and $fchar <= -17418) return "H";
        if ($fchar >= -17417 and $fchar <= -16475) return "J";
        if ($fchar >= -16474 and $fchar <= -16213) return "K";
        if ($fchar >= -16212 and $fchar <= -15641) return "L";
        if ($fchar >= -15640 and $fchar <= -15166) return "M";
        if ($fchar >= -15165 and $fchar <= -14923) return "N";
        if ($fchar >= -14922 and $fchar <= -14915) return "O";
        if ($fchar >= -14914 and $fchar <= -14631) return "P";
        if ($fchar >= -14630 and $fchar <= -14150) return "Q";
        if ($fchar >= -14149 and $fchar <= -14091) return "R";
        if ($fchar >= -14090 and $fchar <= -13319) return "S";
        if ($fchar >= -13318 and $fchar <= -12839) return "T";
        if ($fchar >= -12838 and $fchar <= -12557) return "W";
        if ($fchar >= -12556 and $fchar <= -11848) return "X";
        if ($fchar >= -11847 and $fchar <= -11056) return "Y";
        if ($fchar >= -11055 and $fchar <= -10247) return "Z";
        return null;
    }
}